Ext.define('Fxt.lbc-account.Controller', {
    extend: 'Fxt.core.CrudController',
    stores: 'Fxt.lbc-account.Store',
    models: ['Fxt.lbc-account.Model'],
    views: ['Fxt.lbc-account.view.Viewport', 'Fxt.lbc-account.view.Grid', 'Fxt.lbc-account.view.QueryForm'],
    $onInit: function() {
        var me = this;
        var queryForm = me.$getViewport().$getQueryForm();
        var grid = me.$getViewport().$getGrid();
        var module = me.$getModule();
        this.control({
            'splitbutton[action=new-register]': {
                arrowclick: function(btn) {
                    btn.menu.removeAll();
                    Fxt.ajax({
                        maskCmp: btn,
                        method: 'GET',
                        url: '/lbc/list-mine',
                        success: function(data) {
                            Ext.each(data, function(row) {
                                btn.menu.add({
                                    text: row.mobile + ' - ' + row.realName,
                                    handler: function () {
                                        me.$newRegister(row.inviteCode);
                                    }
                                });
                            });
                            btn.showMenu();
                        }
                    });
                }
            },
            'button[action=sync-invite-code]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/sync-invite-code',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=auth-id]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/auth-id',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=sync-kj]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/sync-kj',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=sync-cp-value]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/sync-cp-value',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=sync-account]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/sync-account',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=profit]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/profit',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=new-jy]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/new-jy',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=lottery]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var mobile = record.get("mobile");
                        Fxt.ajax({
                            method: 'POST',
                            url: '/lbc/lottery',
                            params: {
                                mobile: mobile
                            },
                            success: function () {
                                me.$doQuery();
                            }
                        });
                    });
                }
            }
        });
    },
    $newRegister: function(promotionCode) {
        var me = this;
        Fxt.ajax({
            method: 'POST',
            url: '/lbc/new-register',
            params: {
                promotionCode: promotionCode
            },
            success: function() {
                me.$doQuery();
            }
        });
    },
});